<?php

	/**
	* @package admin
	* @desc Page for sending report to the appropriate council.
	*/
	session_start();
	
	if ($_SESSION["role"] != "admin")
	{
		header('Location: ../index.php');
		exit();
	}
	
	//Direct calling check
	if(!isset($_POST['submit']))	
		die("Do not call this page directly");
	
	
	$action = $_POST['submit'];
	$id = $_POST['id'];
	$user = $_POST['user'];
	$report = $_POST['report'];
	$address = $_POST['address'];
	$council = $_POST['council'];
	$forwarded = $_POST['forwarded'];
	
	$site_admin = $_SESSION['email'];
	
	switch($action)
	{
		case 'Forward Report':
			$mail_subject_council = 'Park Report';
			$message_council = 'Park report: \n\rPark name: '.$park.'\n\r'.$report.'\n\r\n\rMessage sent in behave of '.$user;
			
			$mail_subject_user = 'ParkFind - Park Report sent to Council';
			$message_user = 'Dear ParkFind user,\n\rYour Report:\n\rPark name: '.$park.'\n\r'.$report.'\n\r\n\rhas been forwarded to the correspondent council.\n\r\n\rFor any questions get in contact with the site Administrator '.$site_admin.'\n\rThank You!'; 
			
			$sent_council = mail($council, $mail_subject_council, $message_council, "From: ParkFind <$site_admin>");
			if(!$sent_council)
			{
				die('Could not send proposal to council.');
			}
			
			//update report in db
			$query = "UPDATE reports SET forwarded = 1 WHERE report_id = $id";
			/**
			* @desc This include file connects to the database.
			*/
			require '../includes/connect.inc';
	
			//run query
			$result = mysql_query($query, $connection) or die("Could not update report. Neither council nor user were notified");
		
			//disconnect from db
			mysql_close($connection);
			
			$sent_user = mail($user, $mail_subject_user, $message_user, "From: ParkFind <$site_admin>");
			if(!$sent_user)
			{
				echo 'Could not notify user.\n';
				die('Report has been forwarded to council');
			}
			header('Location: reportsuccess.php');
			break;
			
		case 'Delete Report':
			/**
			* @desc This include file connects to the database.
			*/
			require '../includes/connect.inc';
	
			//delete porposal from db
			$query = "DELETE FROM reports WHERE report_id = $id";
			
			//run query
			$result = mysql_query($query, $connection) or die("Could not delete report.");
		
			//disconnect from db
			mysql_close($connection);
			
			if(!$forwarded)
			{			
				$mail_subject_user = 'ParkFind - Park Report Deleted';
				$message_user = 'Dear ParkFind user,\n\rYour report:\n\rPark: '.$park.'\n\r'.$report.'\n\r\n\rhas been deleted b ParkFind administrators .\n\r\n\rFor any questions get in contact with the site Administrator '.$site_admin.'\n\rThank You!';
			
				$sent_user = mail($user, $mail_subject_user, $message_user, "From: ParkFind <$site_admin>");
				if(!$sent_user)
				{
					echo 'Could not notify user.\n';
					die('Report has been deleted.');
				}
			}
			header('Location: reportdeleted.php');
			break;
			
		case 'Back': 
			header('Location: reports.php');
			exit();
			break;
	}
?>